package com.xxl.job.admin.controller;

import com.xxl.job.admin.dto.CronJobModificationRecordDto;
import com.xxl.job.admin.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @GetMapping("/search")
    public Map<String, Object> searchCronJobAuditLogs(
            @RequestParam(required = false) String serverIp,
            @RequestParam(required = false) String jobDesc,
            @RequestParam(required = false) String modifiedBy,
            @RequestParam(required = false) String command,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<CronJobModificationRecordDto> records = recordService.getCronJobAuditLogs(serverIp, jobDesc, modifiedBy, command, page, size);
        System.out.println(serverIp +" " + jobDesc + " " + modifiedBy + " " + command);
        int total = recordService.getTotalCount(serverIp, jobDesc, modifiedBy, command);

        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("page", page);
        result.put("size", size);
        result.put("total", total);

        return result;
    }
}
